package com.timmy.google_play.holder;

import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.base.MyApplication;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.conf.Constants;
import com.timmy.google_play.manager.DownLoadInfo;
import com.timmy.google_play.manager.DownLoadManager;
import com.timmy.google_play.utils.CommonUtils;
import com.timmy.google_play.utils.PrintDownLoadInfo;
import com.timmy.google_play.utils.StringUtils;
import com.timmy.google_play.utils.UIUtils;
import com.timmy.progressview.ProgressView;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Timmy on 2016/12/8.
 * 1. 提供视图
 * 2. 接收,加载数据
 * 3. 数据和视图的绑定
 * 根据不同的状态 展示不同的 progressview
 * 根据不同的状态 触发不同的操作
 */

public class ItemHolder extends BaseHolder<ItemBean> implements DownLoadManager.DownLoadInfoObserver {

    @BindView(R.id.item_appinfo_iv_icon)
    ImageView    mItemAppinfoIvIcon;
    @BindView(R.id.item_appinfo_tv_title)
    TextView     mItemAppinfoTvTitle;
    @BindView(R.id.item_appinfo_rb_stars)
    RatingBar    mItemAppinfoRbStars;
    @BindView(R.id.item_appinfo_tv_size)
    TextView     mItemAppinfoTvSize;
    @BindView(R.id.item_appinfo_tv_des)
    TextView     mItemAppinfoTvDes;
    @BindView(R.id.item_appinfo_progressview)
    ProgressView mProgressview;
    public ItemBean mData;

    @Override
    protected View initHolderVIew() {
        return View.inflate(UIUtils.getContext(), R.layout.item_home, null);
    }

    @Override
    protected void refreshHolderView(ItemBean data) {

        mData = data;

        //重置progressView进度,避免复用问题
        mProgressview.setProgress(0);

        mItemAppinfoTvDes.setText(data.des);
        mItemAppinfoTvSize.setText(StringUtils.formatFileSize(data.size));
        mItemAppinfoTvTitle.setText(data.name);

        mItemAppinfoRbStars.setRating(data.stars);

        String url = Constants.URLS.IMAGEBASEURL + data.iconUrl;

        Picasso.with(UIUtils.getContext()).load(url).into(mItemAppinfoIvIcon);
        
        /*================ 根据不同的状态,展示不同的ui =================*/
        DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(data);
        refreshProgressViewUI(downLoadInfo);
    }

    @OnClick(R.id.item_appinfo_progressview)
    public void onClick() {
        /*================ 根据不同的状态,触发不同的 =================*/

        DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(mData);

        int curState = downLoadInfo.curState;

        switch (curState) {
            case DownLoadManager.STATE_UNDOWNLOAD:      //未下载
                //去下载
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADING:     //下载中
                //点击暂停
                doPause(downLoadInfo);
                break;
            case DownLoadManager.STATE_PAUSEDOWNLOAD:   //暂停下载
                //点击继续下载
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_WAITINGDOWNLOAD: //等待下载
                doCancle(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADFAILED:  //下载失败
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADED:      //下载完成
                doInstall(downLoadInfo);
                break;
            case DownLoadManager.STATE_INSTALLED:       //安装成功
                doOpen(downLoadInfo);
                break;
        }
    }

    private void refreshProgressViewUI(DownLoadInfo downLoadInfo) {
        int curState = downLoadInfo.curState;

        switch (curState) {
            case DownLoadManager.STATE_UNDOWNLOAD:      //未下载

                //设置按钮文本
                mProgressview.setNote("下载");
                mProgressview.setIcon(R.drawable.ic_download);

                break;
            case DownLoadManager.STATE_DOWNLOADING:     //下载中
                mProgressview.setIcon(R.drawable.ic_pause);
                mProgressview.setProgressEnable(true);

                long max = downLoadInfo.max;
                long progress = downLoadInfo.progress;

                int progress_ = (int) (progress * 1.0f / max * 100 + 0.5f);
                mProgressview.setNote(progress_ + "%");
                mProgressview.setMax(max);
                mProgressview.setProgress(progress);

                break;
            case DownLoadManager.STATE_PAUSEDOWNLOAD:   //暂停下载
                mProgressview.setIcon(R.drawable.ic_resume);
                mProgressview.setNote("继续下载");

                break;
            case DownLoadManager.STATE_WAITINGDOWNLOAD: //等待下载
                mProgressview.setIcon(R.drawable.ic_pause);
                mProgressview.setNote("等待中...");

                break;
            case DownLoadManager.STATE_DOWNLOADFAILED:  //下载失败
                mProgressview.setIcon(R.drawable.ic_redownload);
                mProgressview.setNote("重试");

                break;
            case DownLoadManager.STATE_DOWNLOADED:      //下载完成
                mProgressview.setIcon(R.drawable.ic_install);
                mProgressview.setProgressEnable(false);
                mProgressview.setNote("安装");
                break;
            case DownLoadManager.STATE_INSTALLED:       //安装成功
                mProgressview.setIcon(R.drawable.ic_install);
                mProgressview.setNote("打开");
                break;
        }
    }

    /**
     * 开始下载,断点继续下载,重试下载
     */
    private void doDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().downLoad(downLoadInfo);
    }

    /**
     * 暂停下载
     */
    private void doPause(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().doPause(downLoadInfo);
    }

    /**
     * 取消下载
     */
    private void doCancle(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().doCancle(downLoadInfo);
    }

    /**
     * 安装应用
     */
    private void doInstall(DownLoadInfo downLoadInfo) {
        File apkFile = new File(downLoadInfo.savePath);
        CommonUtils.installApp(UIUtils.getContext(), apkFile);
    }

    /**
     * 打开应用
     */
    private void doOpen(DownLoadInfo downLoadInfo) {
        CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);
    }

    @Override
    public void onDownLoadInfoChange(final DownLoadInfo downLoadInfo) {
        //解决多个下载混乱问题
        if (!downLoadInfo.packageName.equals(mData.packageName)) {
            return;
        }

        //打印当前的状态
        PrintDownLoadInfo.printDownLoadInfo(downLoadInfo);

        //当前线程由发布消息的线程所决定
        //通知按钮更新
        MyApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                refreshProgressViewUI(downLoadInfo);
            }
        });
    }
}
